﻿@model Sealight.Core.DataAdapter.settings
@{
    ViewBag.Title = "Cấu hình";
    Layout = "~/Areas/Admin/Views/Shared/__AdminLayout.cshtml";
}
@section _Head{
    <script type="text/javascript">
        $("#nav-left").accordion("activate", 7);
    </script>
 }
<script language="javascript" type="text/javascript">

    $(document).ready(function () {
        var container = $('div.container_error');
        $("#data_form").validate({
            rules: {
                txtName: {// compound rule 
                    required: true
                }
            },
            messages: {
                txtName: {
                    required: "Vui lòng nhập tên trang web."
                }
            },
            errorElement: "li",
            errorContainer: container,
            errorLabelContainer: $(".error", container)
            ,
            submitHandler: function (form) {

                var data =
            {
                Name: $('#txtName').val(),
                UseCompanyNameInPageTitles: $('#cbUseNameInPageTitles').is(':checked'),
                Email: $('#txtEmail').val(),
                SmtpUserName: $('#txtSmtpUsername').val(),
                SmtpPassword: $('#txtSmtpPassword').val(),
                SmtpServer: $('#txtSmtpServer').val(),
                SmtpServerPort: $('#txtSmtpServerPort').val(),
                UseAuthentication: $('#cbAuthentication').is(':checked'),
                EnableSsl: $('#cbEnableSsl').is(':checked')
            };

                JsUtils.ShowLoader('#data_form', 'Lưu...');
                $.ajax({
                    type: "POST",
                    url: "/admin/Configuration/_SaveSettingsData",
                    data: JSON.stringify(data),
                    traditional: true,
                    dataType: "json",
                    contentType: 'application/json',
                    error: function (xhr, status, error) { },
                    success: function (d) {
                        if (d != null) {
                            if (d.OperationComplete != null && d.OperationComplete == false && d.ErrorCode != null) {
                                if (d.ErrorFriendlyMessage != null) {
                                    ShowErrorMessage(d.ErrorFriendlyMessage);
                                }
                                return;
                            }
                            else {
                                ShowErrorMessage("Lưu thành công", "thông báo");
                            }
                        }
                    },
                    complete: function (d) {
                        JsUtils.hideLoader('#data_form');
                    },
                    cache: false
                }); //end .ajax call
            }
        });

        $('#btnTestEmail').click(function () {

            var data =
            {
                Name: $('#txtName').val(),
                UseCompanyNameInPageTitles: $('#cbUseNameInPageTitles').is(':checked'),
                Email: $('#txtEmail').val(),
                SmtpUserName: $('#txtSmtpUsername').val(),
                SmtpPassword: $('#txtSmtpPassword').val(),
                SmtpServer: $('#txtSmtpServer').val(),
                SmtpServerPort: $('#txtSmtpServerPort').val(),
                UseAuthentication: $('#cbAuthentication').is(':checked'),
                EnableSsl: $('#cbEnableSsl').is(':checked')
            };

            JsUtils.ShowLoader('#data_form', 'test gửi email...');
            $.ajax({
                type: "POST",
                url: "/admin/Configuration/_TestSendMail",
                data: JSON.stringify(data),
                traditional: true,
                dataType: "json",
                contentType: 'application/json',
                error: function (xhr, status, error) { },
                success: function (d) {

                    if (d == 'false') {
                        ShowErrorMessage("Không thể gửi email", "thông báo");

                    } else {
                        ShowErrorMessage("Gửi email thành công", "thông báo");
                    }
                },
                complete: function (d) {
                    JsUtils.hideLoader('#data_form');
                },
                cache: false
            }); //end .ajax call
        });
    });

</script>
<h2>
    Cấu hình</h2>
<form id="data_form" method="post" action="" enctype="multipart/form-data">
<div class="options">
    <input type="submit" value="Lưu" class="adminButtonBlue saveData" title="Lưu" />
</div>
<div class="container_error">
    <h5>
        Vui lòng kiểm tra lại các lỗi...</h5>
    <ol class="error">
    </ol>
</div>
<div class="settings">
    <h1>
        Trang Web</h1>
    <label for="">
        Tên:</label>
    <input type="text" id="txtName" name="txtName" style="width: 300px" value="@Model.Name" /><br />
    <label for="">
        Sử dụng tên cho tiêu đề trang:</label>
    @if (Model.UseCompanyNameInPageTitles)
    {
        <input type="checkbox" runat="server" id="cbUseNameInPageTitles" checked />
    }
    else
    {
        <input type="checkbox" runat="server" id="cbUseNameInPageTitles" />
    }
    <br />
</div>
<div class="settings">
    <h1>
        E-mail</h1>
    <label for="">
        EmailAddress:</label>
    <input type="text" id="txtEmail" style="width: 300px" value="@Model.Email" /><br />
    <label for="">
        SMTP server:</label>
    <input type="text" id="txtSmtpServer" style="width: 300px" value="@Model.SmtpServer"/><br />
    <label for="">
        PortNumber:</label>
    <input type="text" id="txtSmtpServerPort" style="width: 35px" value="@Model.SmtpServerPort"/><br />
    <label for="">
        UserName:</label>
    <input type="text" id="txtSmtpUsername" style="width: 300px" value="@Model.SmtpUserName"/><br />
    <label for="">
        Password:</label>
    <input type="password" id="txtSmtpPassword" style="width: 300px" value="@Model.SmtpPassword"/><br />
    <label for="">
        EnableSsl:</label>
    @if (Model.EnableSsl)
    {
        <input type="checkbox" id="cbEnableSsl" checked />
    
    }
    else
    {
        <input type="checkbox" id="cbEnableSsl" />
    }<br />
    <label for="">
        Use Authentication:</label>
    @if (Model.UseAuthentication)
    {
        <input type="checkbox" id="cbAuthentication" checked />
    
    }
    else
    {
        <input type="checkbox" id="cbAuthentication" />
    }<br />
    <br />
    <div class="options">
        <input type="button" value="Test email" class="adminButtonBlue saveData" id="btnTestEmail"
            title="Test email" />
    </div>
</div>
<div class="options">
    <input type="submit" value="Lưu" class="adminButtonBlue saveData" title="Lưu" />
</div>
</form>
